    //
//  baseboard.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/15.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDBaseBoard.h"
#import "RightPageBtn.h"
#import "KDDatePickerView.h"
#import "KDDateModel.h"
#import "KDTypeBtnBoard.h"
#import "KDTypeBtnBoardModel.h"
#import "KDBaseBoardModel.h"
#import "KDRightPageBtnModel.h"
#import "KDBaseBoardTool.h"
#import "KDHomeThemeModel.h"
#import "KDPatrolGetMyTaskInfoModel.h"
#import "SVProgressHUD.h"
#import "KDSearchView.h"
//#import "KDRGBModel.h"
#define BEGIN_TIME @"beginTime"
#define END_TIME @"endTime"

@interface KDBaseBoard ()<UITextFieldDelegate,KDDatePickerViewDelegate,KDTypeBtnBoardDelegate>
{
    KDBaseBoardModel *_model;
}
@property (weak, nonatomic) IBOutlet RightPageBtn *customBtn;//“自定义”按钮
@property (nonatomic, retain) NSDateFormatter *fmt;
/***  装“媒体类型”板块的view */
@property (weak, nonatomic) IBOutlet UIView *firstView;
/***  装“是否已读”板块的View */
@property (weak, nonatomic) IBOutlet UIView *secondBoard;
/***  装“情感属性”板块的view */
@property (weak, nonatomic) IBOutlet UIView *thirdView;

@property (weak, nonatomic) IBOutlet UIView *datePickerView;//装时间选择的总View
/***  装“信息时间”板块的view */
@property (weak, nonatomic) IBOutlet UIView *fourthView;
/***  装“信息来源”板块的view */
@property (weak, nonatomic) IBOutlet UIView *fifthView;
/**  装是否去重板块的view */
@property (weak, nonatomic) IBOutlet UIView *sixthView;

/***  装“舆情地域”板块的view */
@property (weak, nonatomic) IBOutlet UIView *seventhView;

/***  装“二次检索”板块的view */
@property (weak, nonatomic) IBOutlet UIView *searchUnderView;
@property (weak, nonatomic) IBOutlet KDSearchView *searchView;

@property (weak, nonatomic) IBOutlet UIButton *beginBtn;

@property (weak, nonatomic) IBOutlet UIButton *endBtn;
@property (nonatomic, strong) KDDateModel *dateModel;
/***  时间板块 */
@property (nonatomic, strong) KDTypeBtnBoard *timeBoard;
/***  博客按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *inlandBtn;
/***  信息类型板块的加载View的高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondBoard_Height;
/***  视频按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *videoBtn;
/***  微博按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *mediumBtn;
/***  信息类型板块 */
@property (weak, nonatomic)  KDTypeBtnBoard *sbusinesstypeBtnBoard;
/***  地域范围板块 */
@property (nonatomic, strong) KDTypeBtnBoard *regionBtnBoard;

/***  datePickerView的高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *datePickerViewConstraint_Height;
/*** 确定按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *confirmBtn;
/***  view底层的scrollView */
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/***  scrollView的高度约束，其实是containsize */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollView_constraint_width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollView_constraint_height;
/**
 *  地域板块的高度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *fifthView_Constraint_Height;
/**
 *  信息属性板块的高度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *thirdView_constraint_Height;
/**
 *  那个label的顶端距离thirdView的顶端的高度
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *label_thirdView_Constraint_Top;
/**  舆情地域被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *regionSelectBtn;
/**  信息来源板块被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *sourceFromSelectBtn;

/*** 信息类型页面被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *scopeSelectBtn;

/*** 信息属性页面被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *effectSelectBtn;

/**  已读板块被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *readSelectBtn;

/**  去重被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *dereplicateSelectBtn;
/*** 信息时间页面被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *timeSelectBtn;
/**
 *  当前是不是已经打开了侧边栏
 */
@property (nonatomic, assign, getter=isOpen) BOOL open;
/**
 *  当下打开的DatePickerView
 */
@property (nonatomic, weak) KDDatePickerView *pickView;
@end
@implementation KDBaseBoard

#pragma mark -懒加载区

- (KDBaseBoardModel *)model
{
    if (!_model) {
        _model = [[KDBaseBoardModel alloc]init];
        
    }
    return _model;
}
- (KDDateModel *)dateModel
{
    if (!_dateModel) {
        _dateModel = [[KDDateModel alloc]init];
    }
    return _dateModel;
}
- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"yyyy-MM-dd";
    }
    return _fmt;
}

- (void)setReadSelectBtn:(RightPageBtn *)readSelectBtn{
    _readSelectBtn.selected = NO;
    _readSelectBtn = readSelectBtn;
    _readSelectBtn.selected = YES;
}
- (void)setRegionSelectBtn:(RightPageBtn *)regionSelectBtn{
    _regionSelectBtn.selected = NO;
    _regionSelectBtn = regionSelectBtn;
    _regionSelectBtn.selected = YES;

}

- (void)setSourceFromSelectBtn:(RightPageBtn *)sourceFromSelectBtn{
    _sourceFromSelectBtn.selected = NO;
    _sourceFromSelectBtn = sourceFromSelectBtn;
    _sourceFromSelectBtn.selected = YES;
}

- (void)setEffectSelectBtn:(RightPageBtn *)effectSelectBtn{
    
    _effectSelectBtn.selected = NO;
    _effectSelectBtn = effectSelectBtn;
    _effectSelectBtn.selected = YES;

    
}

- (void)setTimeSelectBtn:(RightPageBtn *)timeSelectBtn{
    _timeSelectBtn.selected = NO;
    _timeSelectBtn = timeSelectBtn;
    _timeSelectBtn.selected = YES;
}

- (void)setDereplicateSelectBtn:(RightPageBtn *)dereplicateSelectBtn{
    _dereplicateSelectBtn.selected = NO;
    _dereplicateSelectBtn = dereplicateSelectBtn;
    _dereplicateSelectBtn.selected = YES;
    
}

- (void)setScopeSelectBtn:(RightPageBtn *)scopeSelectBtn{
    _scopeSelectBtn.selected = NO;
    _scopeSelectBtn = scopeSelectBtn;
    _scopeSelectBtn.selected = YES;
}
# pragma mark - 初始化布局区

//初始化方法
+ (instancetype)baseboard
{
    KDBaseBoard *board = [[[NSBundle mainBundle] loadNibNamed:@"KDBaseBoard" owner:nil options:nil]lastObject];
//    [[NSNotificationCenter defaultCenter]addObserver:board selector:@selector(homeThemeModelChange:) name:@"homeThemeModelDidChangeValueNotification" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:board selector:@selector(currentMyTaskInfoModelChange:) name:@"currentMyTaskInfoModelDidChangeValueNotification" object:nil];
//    board.searchView.radius = 0.03;
//    board.searchView.imageName = @"";
    
//    board.searchView.SearchViewBackGroundColor = WQColor(46, 159, 56);
    board.confirmBtn.clipsToBounds = YES;
    
    //添加手势
//    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
//    [hide addTarget:board action:@selector(hideTheKeyBoard)];
//    [board addGestureRecognizer:hide];
    
    //设置开始按钮和结束按钮的边框风格
    board.beginBtn.layer.borderWidth = 0.6f;
    board.beginBtn.layer.borderColor = WQColor(210 , 210, 210).CGColor;
    board.endBtn.layer.borderWidth = 0.6f;
    board.endBtn.layer.borderColor = WQColor(210 , 210, 210).CGColor;
    [board setUpAll];
    return board;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    //在画之前就把scrollView的宽度定下来
    //    NSLog(@"%f",self.width);
//    NSLog(@"%@",NSStringFromCGRect(self.frame));
    if (self.x != screenW && self.open == NO) {
        self.open = YES;
    }
    if (self.x == screenW) {
        self.open = NO;
    }
//    self.scrollView.contentSize = CGSizeMake(1, screenH - 49);
//    NSLog(@"%@",NSStringFromCGSize(self.scrollView.contentSize));
    [self layoutIfNeeded];

}
- (void)setParentIdstr:(NSString *)parentIdstr{
    _parentIdstr = parentIdstr;
    [self setUpAll];

}
/**
 *  开关的set方法
 */
- (void)setOpen:(BOOL)open{
    _open = open;
    if (open) {//说明正在打开
    }else{
        if (self.pickView) {
            [self.pickView hideDatePickerView];
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
//    NSLog(@"%@",NSStringFromCGRect(self.frame));
    self.scrollView_constraint_width.constant = self.width;
    [self layoutIfNeeded];
}
/**
 *  初始化所有
 */
- (void)setUpAll{
//    NSDictionary *dic = @{@"idstr":self.model.idstr};
//    self.model = nil;
    if (self.model) {
        self.model = [[KDBaseBoardModel alloc]init];
    }
    self.model.regionType = 2;//默认本地
    //获取一周前的日期
    NSDate *beginDate = [[NSDate date] initWithTimeInterval:- (24 *60 * 60 * 6 )sinceDate:[NSDate date]];
    NSDate *endDate = [NSDate date];
    
    self.dateModel.beginDate = beginDate;
    self.dateModel.endDate = endDate;
    [self refreshTitle];
    //给所有的rightBtn值
//    self.scrollView.contentSize = CGSizeMake(1, screenH - 49);
    [self getAllData];
//    [self setUpfifthView];
//    [self setUpThirdView];
    //找到被选中的btn
    self.regionSelectBtn = (RightPageBtn *)[self.seventhView viewWithTag:3];
    self.sourceFromSelectBtn = (RightPageBtn *)[self.fifthView viewWithTag:1];
    self.scopeSelectBtn = (RightPageBtn *)[self.firstView viewWithTag:1];
    self.effectSelectBtn = (RightPageBtn *)[self.thirdView viewWithTag:1];
    self.timeSelectBtn = (RightPageBtn *)[self.fourthView viewWithTag:3];
    self.readSelectBtn = (RightPageBtn *)[self.secondBoard viewWithTag:1];
    
    self.dereplicateSelectBtn = (RightPageBtn *)[self.sixthView viewWithTag:1];
//    self.scopeSelectBtn.selected = YES;self.effectSelectBtn.selected = YES;self.timeSelectBtn.selected = YES;
    [self regionViewBtnClick:self.regionSelectBtn];
    [self sourceFromViewBtnClick:self.sourceFromSelectBtn];
    [self scopeViewBtnClick:self.scopeSelectBtn];
    [self effectBtnClick:self.effectSelectBtn];
    [self readViewBtnClick:self.readSelectBtn];
    [self dereplicationViewBtnClick:self.dereplicateSelectBtn];
    [self timeViewBtnClick:self.timeSelectBtn];
    
    
//    self.beginBtn.enabled = YES;
//    self.endBtn.enabled = YES;
    //如果国内货媒体按钮被点击了，就展开
//    if ([self.scopeSelectBtn.model.sign  isEqualToString: @"1"] || [self.scopeSelectBtn.model.sign isEqualToString:@"3"]) {//如果是国内或媒体按钮
//        [self inlandBtnOrMediumBtnBtnClick];
//    }
}
/**
 *  设置地域板块
 */
- (void)setUpfifthView{
    if (!self.regionBtnBoard) {
        KDTypeBtnBoard *regionBoard = [KDTypeBtnBoard typeBtnBoardWithFrame:CGRectMake(0, 0, self.width, 100)];
        regionBoard.delegate = self;
        self.regionBtnBoard = regionBoard;
        [self.fifthView addSubview:regionBoard];
    }
    KDTypeBtnBoardModel *model = [[KDTypeBtnBoardModel alloc]init];
    model.labelText = @"地域范围";
    self.regionBtnBoard.hidden = NO;
    if (self.showReginType == 0) {//返回为0则不需要显示筛选项
        self.fifthView_Constraint_Height.constant = 0;
        self.regionBtnBoard.height = 0;
        self.regionBtnBoard.hidden = YES;
    }else if (self.showReginType == 1){//返回1时 筛选项为 辖区，本市 ，本省，全国
        self.fifthView_Constraint_Height.constant = 100;
        
        model.btnTitleArr = @[@"辖区", @"本市", @"本省", @"全国"];
        model.btnTagArr = @[@(101), @(102), @(103), @(104)];
//        model.selectBtnTag = self.model.regionType +100;
        
    }else if (self.showReginType == 2){//返回2时 筛选项为 辖区，本省，全国
        self.fifthView_Constraint_Height.constant = 100;
        model.btnTitleArr = @[@"辖区", @"本省", @"全国"];
        model.btnTagArr = @[@(102), @(103), @(104)];
//        model.selectBtnTag = self.model.regionType +100;
    }else if (self.showReginType == 3){//返回1时 筛选项为 辖区，全国
        self.fifthView_Constraint_Height.constant = 100;
        model.btnTitleArr = @[@"辖区", @"全国"];
        model.btnTagArr = @[@(103), @(104)];
//        model.selectBtnTag = self.model.regionType +100;
    }
    [self layoutIfNeeded];
    self.regionBtnBoard.model = model;
    
}
/**
 *  设置信息属性板块
 */
- (void)setUpThirdView{
    if ([self.mainPageType isEqualToString:@"预警"]) {//如果是预警板块，就隐藏掉这个板块
        self.label_thirdView_Constraint_Top.constant = 0;
        self.thirdView_constraint_Height.constant = 0;
        self.thirdView.hidden = YES;
    }
}
//取得所有btn的模型的数据
- (void) getAllData
{
    for (RightPageBtn *btn in self.firstView.subviews) {//信息范畴
        if ([btn isKindOfClass:[RightPageBtn class]]) {
            KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
            //请求需要的参数
            model.type = @"webSrcType";
            model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag -1];
            btn.model = model;
        }
    }
    //信息类型
    for (RightPageBtn *btn in self.thirdView.subviews) {
        if ([btn isKindOfClass:[RightPageBtn class]] ) {
            if (!btn.model) {
                KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
                model.type = @"docType";
                model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag -6];
                btn.model = model;
            }
        }
    }
    for (RightPageBtn *btn in self.fourthView.subviews) {
        if ([btn isKindOfClass:[RightPageBtn class]]) {
            if (!btn.model) {
                KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
                model.type = @"startTime";
                model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag];
                btn.model = model;
            }
        }
    }
}


#pragma mark - 事件点击区
/**
 *  舆情地域板块的按钮被点击了
 *
 *  @param sender <#sender description#>
 */
- (IBAction)regionViewBtnClick:(RightPageBtn *)sender {
    self.regionSelectBtn = sender;
//    self.regionSelectBtn.selected = YES;
    self.model.regionType = sender.tag;
}
/**
 *  信息来源板块的按钮被点击了
 */
- (IBAction)sourceFromViewBtnClick:(RightPageBtn *)sender {
//    self.sourceFromSelectBtn.selected = NO;
    self.sourceFromSelectBtn = sender;
//    self.sourceFromSelectBtn.selected = YES;
    switch (sender.tag) {
        case 1:
            self.model.subtype = nil;
            break;
        case 2:
            self.model.subtype = @"0";
            break;
        case 3:
            self.model.subtype = @"1";
            break;
        default:
            break;
    }
}

/**
 *  媒体类型板块的按钮被点击
 *
 *  @param sender 被点击的按钮
 */
- (IBAction)scopeViewBtnClick:(RightPageBtn *)sender {
//    self.scopeSelectBtn.selected = NO;
    self.scopeSelectBtn = sender;
//    self.scopeSelectBtn.selected = YES;
    //特殊情况：当国内按钮或媒体按钮被点击了，进入这个方法
    //    [self inlandBtnOrMediumBtnBtnClick];

    
    if (sender.tag-1) {
        self.model.webSrcType = [NSString stringWithFormat:@"%ld",sender.tag -1];
    }else{//如果是全部的话就nil
        self.model.webSrcType = nil;
    }
    
}
/**
 *  情感属性板块的按钮被点击了
 *
 *  @param sender 被点击的按钮
 */
- (IBAction)effectBtnClick:(RightPageBtn *)sender {
//    self.effectSelectBtn.selected = NO;
    self.effectSelectBtn = sender;
//    self.effectSelectBtn.selected = YES;
    switch (sender.tag) {
        case 1:
            self.model.docType = nil;
            break;
        case 2:
            self.model.docType = @"1";
            break;
        case 3:
            self.model.docType = @"-1";
            break;
        case 4:
            self.model.docType = @"1";
            break;
        default:
            break;
    }
//    self.model.docType = sender.model.sign;
}
/**
 *  是否已读板块的按钮被点击了
 */
- (IBAction)readViewBtnClick:(RightPageBtn *)sender {
//    self.readSelectBtn.selected = NO;
    self.readSelectBtn = sender;
//    self.readSelectBtn.selected = YES;
    switch (sender.tag) {
        case 1:
            self.model.isRead = nil;
            break;
        case 2:
            self.model.isRead = @"1";
            break;
        case 3:
            self.model.isRead = @"0";
            break;
        default:
            break;
    }
}

/**
 *  去重板块的按钮被点击了
 *
 *  @param sender <#sender description#>
 */
- (IBAction)dereplicationViewBtnClick:(RightPageBtn *)sender {
//    self.dereplicateSelectBtn.selected = NO;
    self.dereplicateSelectBtn = sender;
//    self.dereplicateSelectBtn.selected = YES;
    switch (sender.tag) {
        case 1:
            self.model.filter = nil;
            break;
        case 2:
            self.model.filter = @"0";
            break;
        case 3:
            self.model.filter = @"1";
            break;
        default:
            break;
    }
}


/**
 *  信息时间板块的按钮被点击了
 *
 *  @param sender 被点击的按钮
 */
- (IBAction)timeViewBtnClick:(id)sender{
   
    RightPageBtn *rightBtn = (RightPageBtn *)sender;
//    self.timeSelectBtn.selected = NO;
    self.timeSelectBtn = rightBtn;
//    self.timeSelectBtn.selected = YES;
    self.model.startTime = rightBtn.model.startTime;
    self.model.endTime = rightBtn.model.endTime;
    
    self.model.timeIndex = rightBtn.model.sign;
    self.model.beginDate = rightBtn.model.beginDate;
    self.model.endDate = rightBtn.model.endDate;
    if (self.customBtn.selected) {//如果自定义按钮被选中，那么就打开日期选择器
        [self openDatePicker];
    }
    else{//如果没有被选中，就关闭日期选择器
        [self removeDatePicker];
    }
}

//显示日期选择
- (void)openDatePicker
{
    
    self.datePickerView.hidden = NO;
    [UIView animateWithDuration:0.3f animations:^{
        self.datePickerView.alpha = 1.0f;
        self.datePickerViewConstraint_Height.constant = 40;
        [self layoutIfNeeded];
    }];
    NSDate *curDate = [NSDate date];
    self.curDate = curDate;
    //开始时间
    NSString *begin = [self.fmt stringFromDate:self.dateModel.beginDate];
    //结束时间
    NSString *end = [self.fmt stringFromDate:self.dateModel.endDate];
    self.model.startTime = begin;
    self.model.endTime = end;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, CGRectGetMaxY(self.confirmBtn.frame) +70);
//    NSLog(@"%@",NSStringFromCGSize(self.scrollView.contentSize));
    [self refreshTitle];
}
//隐藏日期选择
- (void)removeDatePicker
{
    [UIView animateWithDuration:0.3f animations:^{
        self.datePickerViewConstraint_Height.constant = 0;
        [self layoutIfNeeded];
        self.datePickerView.alpha = 0;
        self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, CGRectGetMaxY(self.confirmBtn.frame) +70);
    }completion:^(BOOL finished) {
        self.datePickerView.hidden = YES;
        
    }];
    
}
/**
 *  开始时间按钮被点击了
 *
 */
- (IBAction)beginBtnClick:(id)sender {
    [self showPickViewWithType:KDDatePickerTypeBegin];

    
}

//结束时间按钮被点击了
- (IBAction)endBtnClick:(id)sender {
    [self showPickViewWithType:KDDatePickerTypeEnd];

}
/**
 *  type：0:begin；1:end
 */
- (void)showPickViewWithType:(NSInteger)type{
    KDDatePickerView *pickView;
    if (type) {
        pickView = [KDDatePickerView showWithType:type andDate:self.dateModel.endDate];
    }else{
        pickView = [KDDatePickerView showWithType:type andDate:self.dateModel.beginDate];
    }
    
    pickView.delegate = self;
    
}
//国内按钮或媒体按钮被点击了
- (void)inlandBtnOrMediumBtnBtnClick
{
    KDTypeBtnBoardModel *model = [[KDTypeBtnBoardModel alloc]init];
    model.labelText = @"信息类型";
    if (self.inlandBtn.selected || self.mediumBtn.selected) {
        if (self.inlandBtn.selected) {//如果被点中的是国内按钮
            model.btnTitleArr = @[@"全部",@"论坛",@"博客",@"视频",@"微博",@"微信"];
            model.btnTagArr = @[@(10),@(11),@(12),@(13),@(14),@(15)];
            if (self.model) {
                model.selectBtnTag = self.model.subtype.integerValue + 10;
            }else{
                model.selectBtnTag = 12;
                self.model.subtype = @"12";
            }
            
        }else{//如果被点中的是媒体按钮
            model.btnTitleArr = @[@"全部",@"新闻",@"平媒"];
            model.btnTagArr = @[@(10),@(16),@(17)];
            if (self.model) {
                model.selectBtnTag = self.model.subtype.integerValue+10;
            }else{
                model.selectBtnTag = 17;
                self.model.subtype = @"17";
            }
        }
        //如果不存在就建一个
        if (!self.sbusinesstypeBtnBoard) {
            KDTypeBtnBoard *btnBoard = [KDTypeBtnBoard typeBtnBoardWithFrame:CGRectMake(0,0, self.width, 100)];
            self.sbusinesstypeBtnBoard = btnBoard;
            self.sbusinesstypeBtnBoard.delegate = self;
            [self.secondBoard addSubview:btnBoard];
        }
        //让他的约束随着select情况动画起来
            self.secondBoard_Height.constant = 100;
            [self.secondBoard bringSubviewToFront:self.sbusinesstypeBtnBoard];
                self.sbusinesstypeBtnBoard.model = model;
    }
    else{
        [self.sbusinesstypeBtnBoard removeFromSuperview];
            self.secondBoard_Height.constant = 0;
    }
}
/**
 *  确定按钮被点击了
 */
- (IBAction)confirmBtnClick:(RightPageBtn *)sender {
//    self.model.secondSearch = self.searchView.text;
//    self.model.beginDate = [self.fmt dateFromString:self.model.startTime];
//
//    self.model.endDate = [self.fmt dateFromString:self.model.endTime];
    KDBaseBoardModel *model = self.model;
    if (_delegate &&[ _delegate respondsToSelector:@selector( baseBoard:confirmBtnDidClickWithBaseBoardModel:)]) {
        [_delegate baseBoard:self confirmBtnDidClickWithBaseBoardModel:model];
    }
}

- (IBAction)resettingBtnClick:(UIButton *)sender {
    [self setUpAll];
}


#pragma mark - 刷新字体
- (void)refreshTitle
{
    //开始时间
    NSString *begin = [self.fmt stringFromDate:self.dateModel.beginDate];
    //结束时间
    NSString *end = [self.fmt stringFromDate:self.dateModel.endDate];
    [self.beginBtn setTitle:begin forState:UIControlStateNormal];
    [self.endBtn setTitle:end forState:UIControlStateNormal];
}

#pragma mark - KDDatePickerViewDelegate
/**
 *  datepicker的设置按钮被点击了
 *
 *  @param pickerView 时间选择器
 *  @param date       被选中的时间
 *  @param type      确定是开始时间还是结束时间
 */
- (void)datePickerView:(KDDatePickerView *)pickerView settingBtnClickWithDate:(NSDate *)date andType:(KDDatePickerType)type
{
//    self.beginBtn.enabled = YES;
//    self.endBtn.enabled = YES;
    NSComparisonResult result1 = [[NSDate date] compare:date];
    if (result1 == -1) {
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(self.centerX - screenW *0.5, 0)];
        [SVProgressHUD showInfoWithStatus:@"选取时间应小于或等于今天！" maskType:SVProgressHUDMaskTypeGradient];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (type != KDDatePickerTypeBegin) {
                [self endBtnClick:self.endBtn];
            }else{
                [self beginBtnClick:self.beginBtn];
            }
        });
        return;
    }
    if (type == KDDatePickerTypeBegin) {//如果是开始时间被设置了
        //比较开始时间和结束时间，看看哪个大
        NSComparisonResult result = [date compare:self.dateModel.endDate];
        if (result != - 1) {//开始时间大于结束时间
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(self.centerX - screenW *0.5, 0)];
            [SVProgressHUD showInfoWithStatus:@"开始时间应小于结束时间！" maskType:SVProgressHUDMaskTypeGradient];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self endBtnClick:self.endBtn];
            });
            return;
        }else{//开始时间小于结束时间
            self.dateModel.beginDate = date;
        }
    }
    else{
        NSComparisonResult result = [date compare:self.dateModel.beginDate];
        if (result == -1) {//开始时间大于结束时间
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(self.centerX - screenW *0.5, 0)];
            [SVProgressHUD showInfoWithStatus:@"开始时间应小于结束时间！" maskType:SVProgressHUDMaskTypeGradient];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self endBtnClick:self.endBtn];
            });
            return;
        }else{//开始时间小于结束时间
            self.dateModel.endDate = date;
        }
    }
    self.model.beginDate = self.dateModel.beginDate;
    self.model.startTime = [self.fmt stringFromDate:self.dateModel.beginDate];
    self.model.endTime = [self.fmt stringFromDate:self.dateModel.endDate];
    self.model.endDate = self.dateModel.endDate;
    [self refreshTitle];
}

-(void)cancelBtnClick{
//    self.beginBtn.enabled = YES;
//    self.endBtn.enabled = YES;
}
#pragma mark - KDTypeBtnBoardDelegate

- (void)typeBtnBoard:(KDTypeBtnBoard *)typeBtnBoard BtnClickedWithTag:(NSInteger)tag andLabelTitle:(NSString *)tableTitle{

    if ([tableTitle isEqualToString:@"信息类型"]) {
        self.model.subtype = [NSString stringWithFormat:@"%ld",tag-10];
    }else{
//        self.model.regionType = tag -100;
    }
}

#pragma mark - 通知接收区
- (void)homeThemeModelChange:(NSNotification *)noty
{
//    NSLog(@"%@", noty.userInfo);
    KDHomeThemeModel *model = noty.userInfo[@"homeThemeModel"];
    KDBaseBoardModel *baseBoardmodel = [[KDBaseBoardModel alloc]init];
    baseBoardmodel.idstr = [NSString stringWithFormat:@"%ld", (long)model.ID];
    self.model = baseBoardmodel;
    [self setNeedsLayout];
}

- (void)currentMyTaskInfoModelChange:(NSNotification *)noty{
    KDPatrolGetMyTaskInfoModel *model = noty.userInfo[@"currentMyTaskInfoModel"];
    KDBaseBoardModel *baseBoardmodel = [[KDBaseBoardModel alloc]init];
    baseBoardmodel.idstr = [NSString stringWithFormat:@"%ld", (long)model.t_Id];
    self.model = baseBoardmodel;
    [self setNeedsLayout];

}
#pragma mark - 隐藏键盘
- (void)hideTheKeyBoard
{
    [self endEditing:YES];
}
@end
